package com.emptyMe.org.controller;

import com.emptyMe.basic.utils.AjaxResult;
import com.emptyMe.org.domain.Shop;
import com.emptyMe.org.query.ShopQuery;
import com.emptyMe.org.service.IShopService;
import com.emptyMe.org.service.imp.ShopServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口", description = "店铺接口详细api描述")
public class ShopController {
    @Autowired
    private IShopService service;
//    ShopServiceImpl service;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    /**
     * 添加或更新 用id判断
     *
     * @param :Shop
     * @return RequestBody : 表示获取请求提的json数据
     */
    /**/
    @PutMapping
    @ApiOperation(value = "店铺添加或修改", notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop s) {
        if (s.getId() == null) {
            try {
                service.save(s);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false, "添加失败-" + e.getCause());
            }
        } else {
            try {
                service.update(s);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false, "修改失败-" + e.getCause());
            }
        }
    }

    @DeleteMapping("/{id}")  // shop/2 shop/3
    @ApiOperation(value = "店铺删除", notes = "删除该id下的店铺")
//    public AjaxResult delete(@RequestBody Long id) {
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            service.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false, "删除失败--" + e.getCause());
        }
    }


    @DeleteMapping("/delMany")
    @ApiOperation(value = "店铺批量删除", notes = "批量删除店铺")
    public AjaxResult deleteMany(@RequestBody List<Shop> shops) {
        try {
            service.removeMany(shops);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false, "删除失败--" + e.getCause());
        }
    }


    // axios 不支持传对象
    @PostMapping
    @ApiOperation(value = "分页＋模糊查询")
    public AjaxResult list(@RequestBody ShopQuery query) {
        PageInfo<Shop> info = service.searchData(query);
        return AjaxResult.me(info);
    }

    @PostMapping("upload")
    @ApiOperation(value = "上传图片")  // 店铺入驻Logo上传
    public AjaxResult upload(MultipartFile file) {
        try {
            String end = file.getOriginalFilename().split("[.]")[1];
            StorePath path = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), end, null);
//            System.out.println(path);
            if (path!=null){
                return AjaxResult.me(path);
            }
            return AjaxResult.me(false,"上传失败");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me(false,"上传失败");
        }
    }
}
